package com.ruoyi.question.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.question.domain.QueQuestions;
import com.ruoyi.question.mapper.QueQuestionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.question.mapper.QueListDetailsMapper;
import com.ruoyi.question.domain.QueListDetails;
import com.ruoyi.question.service.IQueListDetailsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 清单细则Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-03
 */
@Service
public class QueListDetailsServiceImpl implements IQueListDetailsService 
{
    @Autowired
    private QueListDetailsMapper queListDetailsMapper;

    @Autowired
    private QueQuestionsMapper queQuestionsMapper;

    /**
     * 查询清单细则
     * 
     * @param detailsId 清单细则主键
     * @return 清单细则
     */
    @Override
    public QueListDetails selectQueListDetailsByDetailsId(String detailsId)
    {
        return queListDetailsMapper.selectQueListDetailsByDetailsId(detailsId);
    }

    /**
     * 查询清单细则列表
     * 
     * @param queListDetails  清单细则
     * @return 清单细则
     */
    @Override
    public List<QueQuestions> selectQueListDetailsList(QueListDetails queListDetails)
    {
        //创建查询对象
        QueQuestions questions = new QueQuestions();
        //获取题库所有题
        List<QueQuestions> queQuestionsList = queQuestionsMapper.selectQueQuestionsList(questions);
        //获取详情，根据前段返回的对象进行查询，mybatis会根据UUID拼成动态sql查询需要查的清单
        List<QueListDetails> queListDetailsList = queListDetailsMapper.selectQueListDetailsList(queListDetails);

        //创建一个list用来存放当前清单的UUID
        List<String> queIdList = new ArrayList<>();
        //遍历当前清单已添加的问题
        if (!queListDetailsList.isEmpty()){
            for (QueListDetails listDetails : queListDetailsList) {
                //存入list
                queIdList.add(listDetails.getQuestionId());
            }
        }

        //如果当前清单已有问题，进行两个for循环遍历
        if (!queIdList.isEmpty()){
            for (QueQuestions queQuestions : queQuestionsList) {
                for (String s : queIdList) {
                    //如果出现，则记录
                    if (queQuestions.getId().equals(s)){
                        queQuestions.setBackupField1("1");
                    }
                }
                if (queQuestions.getBackupField1() == null || queQuestions.getBackupField1().equals("")){
                    queQuestions.setBackupField1("0");
                }
            }
        }

        return queQuestionsList;
    }

    /**
     * 新增清单细则
     * 
     * @param queListDetails 清单细则
     * @return 结果
     */
    @Override
    public int insertQueListDetails(QueListDetails queListDetails)
    {
        return queListDetailsMapper.insertQueListDetails(queListDetails);
    }

    /**
     * 修改清单细则
     * 
     * @param queListDetails 清单细则
     * @return 结果
     */
    @Override
    public int updateQueListDetails(QueListDetails queListDetails)
    {
        return queListDetailsMapper.updateQueListDetails(queListDetails);
    }

    /**
     * 批量删除清单细则
     * 
     * @param detailsIds 需要删除的清单细则主键
     * @return 结果
     */
    @Override
    public int deleteQueListDetailsByDetailsIds(String detailsIds)
    {
        return queListDetailsMapper.deleteQueListDetailsByDetailsIds(Convert.toStrArray(detailsIds));
    }

    /**
     * 删除清单细则信息
     * 
     * @param detailsId 清单细则主键
     * @return 结果
     */
    @Override
    public int deleteQueListDetailsByDetailsId(String detailsId)
    {
        return queListDetailsMapper.deleteQueListDetailsByDetailsId(detailsId);
    }
}
